﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Studio.Foundation.Ontologies;
using System.Xml;
using System.IO;
using System.Xml.Schema;

public partial class EntityView_GetType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Assembly assembly = CACHED_ASSEMBLY;
        string t = ("Studio.Web.WebControls.EntityView");
        Type type = Type.GetType(t);
        Type t1 = assembly.GetType("Studio.Xml.Entity.ButtonType");
        type = FindTypeByTypeName("ButtonType");
        Console.Write(type.Name);
    }


    public static Type FindTypeByTypeName(string typeName)
    {
        Assembly assembly = CACHED_ASSEMBLY;
        System.Diagnostics.Debug.Assert(null == assembly);
        if (null == assembly) return null;

        Type[] types = assembly.GetTypes();
        foreach (Type type in types)
            if (type.Name == typeName || type.FullName == typeName || type.AssemblyQualifiedName == typeName)
                return type;
        return null;
    }

    static Assembly _CACHED_ASSEMBLY;
    /// <summary>
    /// The compiled assembly of entities
    /// </summary>
    public static Assembly CACHED_ASSEMBLY
    {
        get
        {
            if (null == _CACHED_ASSEMBLY)
                CompileAssembly();

            return _CACHED_ASSEMBLY;
        }
    }

    public static void CompileAssembly()
    {
        // validate schemas
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        foreach (string schemaText in schemas)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(schemaText);
            XmlReader reader = new XmlTextReader(new StringReader(doc.OuterXml));
            // Read the xsd
            XmlSchema xsd = schemaSet.Add(null, reader);
        }
        schemaSet.Compile();

        // Create xsd compiler
        XsdCompiler compiler = new XsdCompiler(null, null, null);

        // Compile the schemas to assembly
        foreach (XmlSchema xsd in schemaSet.Schemas())
            compiler.AddSchema(xsd);
        _CACHED_ASSEMBLY = compiler.Compile();
    }


    static string[] schemas = 
    {
        @"<?xml version=""1.0""?>
        <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
        <xs:element name=""ButtonType"">
          <xs:complexType>
            <xs:sequence>
              <xs:element name=""名称"" type=""xs:string""/>
              <xs:element name=""文字"" type=""xs:string""/>
              <xs:element name=""宽度"" type=""xs:int""/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        </xs:schema>",

        @"<?xml version=""1.0""?>
        <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">

        <xs:element name=""MenuType"">
          <xs:complexType>
            <xs:sequence>
              <xs:element name=""名称"" type=""xs:string""/>
              <xs:element name=""是否激活"" type=""xs:boolean""/>
              <xs:element name=""图标"" type=""xs:string""/>
              <xs:element name=""显示序号"" type=""xs:int""/>
              <xs:element name=""Url"" type=""xs:string""/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        </xs:schema>"
    };

}